package networkinstance;

public class MultiThreadServer implements Runnable {
    java.net.Socket csocket;

    MultiThreadServer(java.net.Socket csocket) {
        this.csocket = csocket;
    }

    public static void main(String[] args) {
        java.net.ServerSocket ssock = null;
        try {
            ssock = new java.net.ServerSocket(1234);
            System.out.println("Listening");
            while (true) {
                java.net.Socket sock = ssock.accept();
                System.out.println("Connected");
                new Thread(new MultiThreadServer(sock)).start();
            }
        } catch (java.io.IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void run() {

        try {
            java.io.PrintStream pstream = new java.io.PrintStream(csocket.getOutputStream());
            for (int i = 100; i >= 0; i--) {
                pstream.println(i + " bottles of beer on the wall");
            }
            pstream.close();
            csocket.close();
        } catch (java.io.IOException e) {
            System.out.println(e);
        }
    }
}
